package com.hncc.sms.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.hncc.sms.domain.SmsSkdRule;
import com.hncc.sms.domain.SmsSkdRuleItem;
import com.hncc.sms.domain.vo.SmsSkdVO;
import com.hncc.sms.mapper.SmsSkdRuleItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hncc.sms.mapper.SmsSkdMapper;
import com.hncc.sms.domain.SmsSkd;
import com.hncc.sms.service.ISmsSkdService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 查看排班表Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-08-21
 */
@Service
public class SmsSkdServiceImpl implements ISmsSkdService 
{
    @Resource
    private SmsSkdMapper smsSkdMapper;

    @Resource
    private SmsSkdRuleItemMapper smsSkdRuleItemMapper;

    /**
     * 查询查看排班表
     * 
     * @param id 查看排班表ID
     * @return 查看排班表
     */
    @Override
    public SmsSkd selectSmsSkdById(Long id)
    {
        return smsSkdMapper.selectSmsSkdById(id);
    }

    /**
     * 查询查看排班表列表
     * 
     * @param smsSkd 查看排班表
     * @return 查看排班表
     */
    @Override
    public List<SmsSkd> selectSmsSkdList(SmsSkd smsSkd)
    {
        return smsSkdMapper.selectSmsSkdList(smsSkd);
    }

    /**
     * 新增查看排班表
     * 
     * @param smsSkdRule 查看排班表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSmsSkd(SmsSkdRule smsSkdRule)
    {
        //查询两个日期之间的所有日期
        List<Date> dateList = dateSplit(smsSkdRule.getStartDate(),smsSkdRule.getEndDate());
        //根据排班规则id查询排班规则明细
        SmsSkdRuleItem smsSkdRuleItem = new SmsSkdRuleItem();
        //拿到排班规则id
        smsSkdRuleItem.setSkRuleId(smsSkdRule.getId());
        List<SmsSkdRuleItem> smsSkdRuleItems = smsSkdRuleItemMapper.selectSmsSkdRuleItemList(smsSkdRuleItem);
        int result = 0;
        //遍历规则明细
        for (int i = 0; i < smsSkdRuleItems.size(); i++){
            //将排班时间转为数组
            String[] daysOfWeek = smsSkdRuleItems.get(i).getDaysOfWeek().split("");
            //遍历日期
            for (int j = 0; j < dateList.size(); j++){
                Calendar cal = Calendar.getInstance();
                cal.setTime(dateList.get(j));
                //拿到当前日期是星期几
                int week = cal.get(Calendar.DAY_OF_WEEK)-1;
                //遍历排班时间
                for (int z = 0; z < daysOfWeek.length; z++){
                    SmsSkd smsSkd = new SmsSkd();
                    smsSkd.setDate(dateList.get(j));
                    smsSkd.setRemain(smsSkdRuleItems.get(i).getSkLimit());
                    //0：上午，1：下午
                    if (z % 2 == 0) {
                        smsSkd.setNoon(0);
                    } else {
                        smsSkd.setNoon(1);
                    }
                    smsSkd.setStaffId(smsSkdRuleItems.get(i).getUserId());
                    smsSkd.setDeptId(smsSkdRule.getDeptId());
                    smsSkd.setSkLimit(smsSkdRuleItems.get(i).getSkLimit());
                    if(week == 1 && Integer.valueOf(daysOfWeek[z]) == 1 && z < 2){
                        result = deleteAndInsert(smsSkd);
                    }else if(week == 2 && Integer.valueOf(daysOfWeek[z]) == 1 && z < 4 && z >= 2){
                        result = deleteAndInsert(smsSkd);
                    }else if(week == 3 && Integer.valueOf(daysOfWeek[z]) == 1 && z < 6 && z >= 4){
                        result = deleteAndInsert(smsSkd);
                    }else if(week == 4 && Integer.valueOf(daysOfWeek[z]) == 1 && z < 8 && z >= 6){
                        result = deleteAndInsert(smsSkd);
                    }else if(week == 5 && Integer.valueOf(daysOfWeek[z]) == 1 && z < 10 && z >= 8){
                        result = deleteAndInsert(smsSkd);
                    }else if(week == 6 && Integer.valueOf(daysOfWeek[z]) == 1 && z < 12 && z >= 10){
                        result = deleteAndInsert(smsSkd);
                    }else if(week == 0 && Integer.valueOf(daysOfWeek[z]) == 1 && z < 14 && z >= 12){
                        result = deleteAndInsert(smsSkd);
                    }
                }
            }
            //当日期范围内没有医生上班返回-1
            if(i == smsSkdRuleItems.size()-1 && result == 0){
                result = -1;
            }
        }
        return result;
    }

    /**
     * 查询两个日期之间的所有日期
     * @param startDate   开始日期
     * @param endDate   结束日期
     * @return
     * @throws Exception
     */
    private List<Date> dateSplit(Date startDate, Date endDate) {
        Long spi = endDate.getTime() - startDate.getTime();
        // 相隔天数
        Long step = spi / (24 * 60 * 60 * 1000);
        List<Date> dateList = new ArrayList<Date>();
        dateList.add(endDate);
        for (int i = 1; i <= step; i++) {
            // 比上一天减一
            dateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));
        }
        return dateList;
    }

    /**
     * 先删除在添加
     * @param smsSkd
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteAndInsert(SmsSkd smsSkd){
        smsSkdMapper.deleteSmsSkdByDateAndUserIdAndNoon(smsSkd);
        return smsSkdMapper.insertSmsSkd(smsSkd);
    }

    /**
     * 修改查看排班表
     * 
     * @param smsSkd 查看排班表
     * @return 结果
     */
    @Override
    public int updateSmsSkd(SmsSkd smsSkd)
    {
        return smsSkdMapper.updateSmsSkd(smsSkd);
    }

    /**
     * 批量删除查看排班表
     * 
     * @param ids 需要删除的查看排班表ID
     * @return 结果
     */
    @Override
    public int deleteSmsSkdByIds(Long[] ids)
    {
        return smsSkdMapper.deleteSmsSkdByIds(ids);
    }

    /**
     * 删除查看排班表信息
     * 
     * @param id 查看排班表ID
     * @return 结果
     */
    @Override
    public int deleteSmsSkdById(Long id)
    {
        return smsSkdMapper.deleteSmsSkdById(id);
    }

    @Override
    public List<SmsSkdVO> getSmsSkdList(SmsSkdVO smsSkdVO) {
        return smsSkdMapper.getSmsSkdList(smsSkdVO);
    }
}
